package com.truyen.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.googlecode.ehcache.annotations.Cacheable;
import com.truyen.persistence.dao.ChapterTmpTruyenTranhDao;
import com.truyen.persistence.entity.ChapterTmpTruyenTranh;
import com.truyen.persistence.entity.TruyenTranh;
import com.truyen.service.ChapterTmpTruyenTranhService;
import com.truyen.utils.PagingInfo;


@Service
public class ChapterTmpTruyenTranhServiceImpl implements ChapterTmpTruyenTranhService {
	
	@Autowired
	private ChapterTmpTruyenTranhDao chapterTmpTruyenTranhDao;
	
	@Override
	public void save(ChapterTmpTruyenTranh entity) {
		chapterTmpTruyenTranhDao.save(entity);
	}

	@Override
	@Cacheable(cacheName="chapterTmpTruyenTranhByIdCacheDataController")
	public ChapterTmpTruyenTranh findById(Long id) {
		return chapterTmpTruyenTranhDao.findById(id);
	}

	@Override
	public List<ChapterTmpTruyenTranh> getAll() {
		return chapterTmpTruyenTranhDao.findAll();
	}

	@Override
	public ChapterTmpTruyenTranh findByProperty(String column, Object value) {
		List<ChapterTmpTruyenTranh> lst = chapterTmpTruyenTranhDao.findByQuery(column, value);
		return (lst.size() > 0)?lst.get(0):null;
	}

	@Override
	public List<ChapterTmpTruyenTranh> getLstChapterByTruyen(TruyenTranh entity, String kwSearch, String sort, String sortBy, PagingInfo pInfo) {
		return chapterTmpTruyenTranhDao.getLstChapterByTruyen(entity,kwSearch,sort,sortBy,pInfo);
	}

	@Override
	public ChapterTmpTruyenTranh findAliasvTruyen(TruyenTranh entity, String alias) {
		return chapterTmpTruyenTranhDao.findAliasvTruyen(entity, alias);
	}

	@Override
	@Cacheable(cacheName="chapterTmpTruyenTranhAllCacheDataController")
	public List<ChapterTmpTruyenTranh> getLstAllChapterTmpTruyenTranh(String kwSearch, String sort, String sortBy, PagingInfo pInfo) {
		return chapterTmpTruyenTranhDao.getLstAllChapterTmpTruyenTranh(kwSearch, sort, sortBy, pInfo);
	}

	@Override
	public void updateChapterNumber(Long id, float chapterNumberChange) {
		try{
			ChapterTmpTruyenTranh chapter = findById(id);
			chapter.setChapterNumber(chapterNumberChange);
			chapterTmpTruyenTranhDao.merge(chapter);
		}catch (Exception e) {
			// TODO: handle exception
		}
		
	}

	@Override
	@Cacheable(cacheName="chapterTmpTruyenTranhCacheTruyenController")
	public List<ChapterTmpTruyenTranh> getByTruyen(TruyenTranh entity) {
		return chapterTmpTruyenTranhDao.getByTruyen(entity);
	}

	@Override
	public ChapterTmpTruyenTranh getChapterByTruyenChapnumber(TruyenTranh entity, float chapterNumber) {
		return chapterTmpTruyenTranhDao.getChapterByTruyenChapnumber(entity,chapterNumber);
	}

	@Override
	@Transactional
	public void delete(long id) {
		try{
			ChapterTmpTruyenTranh chapter = findById(id);
			if(chapter != null){
				chapterTmpTruyenTranhDao.delete(chapter);
			}
		}catch (Exception e) {
			// TODO: handle exception
		}
	}

	@Override
	@Cacheable(cacheName="chapterTmpTruyenTranhMoiCapNhatCacheTruyenController")
	public List<ChapterTmpTruyenTranh> getListNewChapter(int maxLimit) {
		return chapterTmpTruyenTranhDao.getListNewChapter(maxLimit);
	}
	
	@Override
	public void update(Long id, Float chapterNumber) {
		try{
			ChapterTmpTruyenTranh chapter = findById(id);
			if(chapter != null){
				chapter.setChapterNumber(chapterNumber);
				chapterTmpTruyenTranhDao.merge(chapter);
			}
		}catch (Exception e) {
			// TODO: handle exception
		}
	}

	@Override
	public List<ChapterTmpTruyenTranh> getLstByOrderBy(String columnOrderBy, PagingInfo pInfo) {
		return chapterTmpTruyenTranhDao.getLstByOrderBy(columnOrderBy, pInfo);
	}

	@Override
	public List<ChapterTmpTruyenTranh> getByTruyenPaging(TruyenTranh entity, PagingInfo pInfoChapter) {
		return chapterTmpTruyenTranhDao.getByTruyenPaging(entity,pInfoChapter);
	}

	@Override
	public ChapterTmpTruyenTranh findByTruyenChapterNumber(TruyenTranh entity, float chapterNumber) {
		return chapterTmpTruyenTranhDao.findByTruyenChapterNumber(entity,chapterNumber);
	}

	@Override
	public List<Object> getByIdTruyen(Long idTruyen) {
		return chapterTmpTruyenTranhDao.getByIdTruyen(idTruyen);
	}

}
